package com.llj.Road_curve_measuring_system.module.input;


import android.os.Handler;
import android.view.View;

import com.llj.Road_curve_measuring_system.base.BaseActivity;
import com.llj.Road_curve_measuring_system.common.GlobalConstant;
import com.llj.Road_curve_measuring_system.databinding.DataPageBinding;
import com.llj.Road_curve_measuring_system.module.results.ResultActivity;
import com.llj.Road_curve_measuring_system.util.ActivityUtil;




public class InputActivity extends BaseActivity<InputPresenter> implements InputView{

    /**
     * 初始化布局Binding
     */
    protected DataPageBinding binding;

    //输入框获取到的数据初始化
    String data1;
    String data2;
    String min;
    String r;
    String jd;
    String interval;
    String xxx;
    String jdx;
    String jdy;
    String jdZy;

    @Override
    protected InputPresenter createPresenter() {
        return new InputPresenter(this);
    }

    @Override
    protected void initView() {
        data1 = ActivityUtil.getIntentData();
        data2 = ActivityUtil.getIntentSecondData();
        if (data1.equals(GlobalConstant.CIRCULAR_CURVE)){
            binding.textView5EaseCurveLength.setVisibility(View.GONE);
            binding.tilEaseCurveLength.setVisibility(View.GONE);
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                binding.textView6JdCorX.setVisibility(View.GONE);
                binding.textView7JdCorY.setVisibility(View.GONE);
                binding.textView8JdToZyAz.setVisibility(View.GONE);
                binding.tilJdCorX.setVisibility(View.GONE);
                binding.tilJdCorY.setVisibility(View.GONE);
                binding.tilJdToZyAz.setVisibility(View.GONE);
                binding.buttonCalculation.setOnClickListener(view ->{
                    loadEditCc(GlobalConstant.CCDM);
                    showLoading();
                    //为出现好的动画效果，延迟1s执行
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            presenter.calculation(GlobalConstant.CCDM,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }
                    },1000);

                });
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                binding.textView6JdCorX.setVisibility(View.GONE);
                binding.textView7JdCorY.setVisibility(View.GONE);
                binding.textView8JdToZyAz.setVisibility(View.GONE);
                binding.tilJdCorX.setVisibility(View.GONE);
                binding.tilJdCorY.setVisibility(View.GONE);
                binding.tilJdToZyAz.setVisibility(View.GONE);
                binding.buttonCalculation.setOnClickListener(view ->{
                    loadEditCc(GlobalConstant.CCTS);
                    showLoading();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            presenter.calculation(GlobalConstant.CCTS,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }
                    },1000);

                });
            }else {
                //这里不用调隐藏方法 setVisibility 直接设置bt的方法就行
                binding.buttonCalculation.setOnClickListener(view -> {
                    loadEditCc(GlobalConstant.CCPV);
                    showLoading();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (data2.equals(GlobalConstant.COORDINATE_STAKING)){
                                presenter.calculation(GlobalConstant.CCCS,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                            }else {
                                presenter.calculation(GlobalConstant.CCPV,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                            }
                        }
                    },1000);
                });
            }
        }else {
            if (data2.equals(GlobalConstant.DECLINATION_METHOD)){
                binding.textView6JdCorX.setVisibility(View.GONE);
                binding.textView7JdCorY.setVisibility(View.GONE);
                binding.textView8JdToZyAz.setVisibility(View.GONE);
                binding.tilJdCorX.setVisibility(View.GONE);
                binding.tilJdCorY.setVisibility(View.GONE);
                binding.tilJdToZyAz.setVisibility(View.GONE);
                binding.buttonCalculation.setOnClickListener(view ->{
                    loadEditMc(GlobalConstant.CCDM);
                    showLoading();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            presenter.calculation(GlobalConstant.MCDM,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }
                    },1000);

                });
            }else if (data2.equals(GlobalConstant.TANGENT_SUPPORT_METHOD)){
                binding.textView6JdCorX.setVisibility(View.GONE);
                binding.textView7JdCorY.setVisibility(View.GONE);
                binding.textView8JdToZyAz.setVisibility(View.GONE);
                binding.tilJdCorX.setVisibility(View.GONE);
                binding.tilJdCorY.setVisibility(View.GONE);
                binding.tilJdToZyAz.setVisibility(View.GONE);
                binding.buttonCalculation.setOnClickListener(view ->{
                    loadEditMc(GlobalConstant.MCTS);
                    showLoading();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            presenter.calculation(GlobalConstant.MCTS,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }
                    },1000);

                });
            }else {
                //这里不用调隐藏方法 setVisibility 直接设置bt的方法就行
            binding.buttonCalculation.setOnClickListener(view -> {
                loadEditMc(GlobalConstant.MCPV);
                showLoading();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (data2.equals(GlobalConstant.COORDINATE_STAKING)){
                            presenter.calculation(GlobalConstant.MCCS,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }else {
                            presenter.calculation(GlobalConstant.MCPV,min,r,jd,interval,xxx,jdx,jdy,jdZy);
                        }
                    }
                },1000);

            });
            }
        }

    }

    /**
     * 对输入框的数据验证绑定
     */
    @Override
    protected void initData() {
        InputTextWatcher textWatcher;
        textWatcher = new InputTextWatcher(binding.tilCircumferentialRadius,
                binding.til2TurningAngle,
                binding.tilNodeNumber,binding.tilDistance,binding.tilEaseCurveLength,binding.tilJdCorX,binding.tilJdCorY,binding.tilJdToZyAz);
        binding.tilCircumferentialRadiusT.addTextChangedListener(textWatcher);
        binding.til2TurningAngleT.addTextChangedListener(textWatcher);
        binding.tilNodeNumberT.addTextChangedListener(textWatcher);
        binding.tilDistanceT.addTextChangedListener(textWatcher);
        binding.tilEaseCurveLengthT.addTextChangedListener(textWatcher);
        binding.tilJdCorXT.addTextChangedListener(textWatcher);
        binding.tilJdCorYT.addTextChangedListener(textWatcher);
        binding.tilJdToZyAzT.addTextChangedListener(textWatcher);
    }

    @Override
    protected View getRootView() {
        binding = DataPageBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    /**
     * 载入来自于圆曲线界面的的数据
     * @param path path
     */
    protected void loadEditCc(int path){
        min = binding.til2TurningAngleT.getText().toString().trim();
        r = binding.tilCircumferentialRadiusT.getText().toString().trim();
        jd = binding.tilNodeNumberT.getText().toString().trim();
        interval = binding.tilDistanceT.getText().toString().trim();
        if (path == GlobalConstant.CCPV){
            jdx = binding.tilJdCorXT.getText().toString().trim();
            jdy = binding.tilJdCorYT.getText().toString().trim();
            jdZy = binding.tilJdToZyAzT.getText().toString().trim();
        }
    }

    /**
     * 载入来自于缓和曲线界面的数据
     * @param path path
     */
    protected void loadEditMc(int path){
        min = binding.til2TurningAngleT.getText().toString().trim();
        r = binding.tilCircumferentialRadiusT.getText().toString().trim();
        jd = binding.tilNodeNumberT.getText().toString().trim();
        interval = binding.tilDistanceT.getText().toString().trim();
        xxx = binding.tilEaseCurveLengthT.getText().toString().trim();
        if (path == GlobalConstant.MCPV){
            jdx = binding.tilJdCorXT.getText().toString().trim();
            jdy = binding.tilJdCorYT.getText().toString().trim();
            jdZy = binding.tilJdToZyAzT.getText().toString().trim();
        }
    }

    @Override
    public void doSuccess() {
            ActivityUtil.actionSecondStart(ResultActivity.class,data1,data2);
    }

}